package com.org.khtn.command.select;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.text.Utilities;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.org.khtn.command.QueryPattern;
import com.org.khtn.data.Data;
import com.org.khtn.data.DataTables;
import com.org.khtn.ultilities.Ultilities;

public class From extends Command {
	final String className = "From()";

	public From() {
		resultData = new HashMap<String, Multimap<String, String>>();
		data = new HashMap<String, Map<String, Map<String, String>>>();
		tableDefine = new HashMap<String, Map<String, String>>();
	}

	public Join from(String key) {

		final String methodName = "from():";
	
		if (key == null) {
			Join join = new Join();
			join.putData(resultData);
			join.setTables(tables);
			join.setData(data);
			join.setTableDefine(tableDefine);
			join.putAllCurrentTable(currentTable);
			return join;

		}
		Map<String, Multimap<String, String>> result = new HashMap<String, Multimap<String, String>>();
		String[] myTable = key.split(",");
		if (key.matches(QueryPattern.SELECT_PATTERN)) {
			System.out.println(className + methodName + "key: " + key);
			SelectQuery a =new SelectQuery(key, databaseName);
			result = a.executeReader();
		} else {
			int length = myTable.length;
			for (String t : myTable) {
				currentTable.add(t);
			}
			if (length == 1)
				result = fromOneTable(myTable[0]);
			else if (length > 1)
				result = fromTables(myTable);
			else
				result = null;
		}
		System.out.println(className + methodName + "table.length"
				+ tables.length);

		Join join = new Join();
		join.putData(result);
		join.setTables(tables);
		join.setData(data);
		join.databaseName = databaseName;
		join.setTableDefine(tableDefine);
		join.putAllCurrentTable(currentTable);
		return join;
	}

	public Map<String, Multimap<String, String>> fromOneTable(String key) {
		Map<String, Map<String, String>> result= new HashMap<String, Map<String,String>>();
		DataTables dt = new DataTables(new Data(databaseName, "sadsa", "mytest\\data\\"));
		if (dt.tableExist(key)==-1)
			return Ultilities.createError("Table not exist");
		else result = dt.getAllRow(key);
		return Ultilities.mapToMultimap(result);
	}

	public Map<String, Multimap<String, String>> fromTables(String[] tables) {
		final String methodName = "fromTables():";

		Map<String, Multimap<String, String>> result = new HashMap<String, Multimap<String, String>>();
		int length = tables.length;
		Map<String, Multimap<String, String>> table1 = fromOneTable(tables[0]);
		Map<String, Multimap<String, String>> table2 = fromOneTable(tables[1]);
		if (table1.containsKey("error"))
			return table1;
		if (table2.containsKey("error"))
			return table2;
		
		result = Ultilities.mul(table1, table2);
		if (result == null)
			return null;
		for (int i = 2; i < length - 1; i++) {
			table1 = fromOneTable(tables[i]);

			result = Ultilities.mul(result, table1);
			if (result == null)
				return null;

		}

		return result;
	}

}
